package com.webgis.demo.server;
import com.webgis.demo.DAO.MapDao;
import com.webgis.demo.DAO.MapSqlDao;
import com.webgis.demo.DAO.SectionDao;
import com.webgis.demo.DAO.SectionSqlDao;
import com.webgis.demo.code.Result;
import com.webgis.demo.pojo.Map;
import com.webgis.demo.pojo.Section;
import com.webgis.demo.pojo.SectionMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service

public class MapServer {
    @Autowired
    MapDao mapDao;
    @Autowired
    MapSqlDao mapSqlDao;
    @Autowired
    SectionDao sectionDao;
    @Autowired
    SectionSqlDao sectionSqlDao;
    public List<Map> getMap(){
        return mapDao.findAll();
    }
    public Result addMapPoint(Map m){
        int count=0;
        if(mapDao.findAllById(m.getId()).isEmpty()){
           count=mapSqlDao.addMapPoint(m);
        }
        else {
            count=mapSqlDao.Update(m);
        }
        if(count>0)
            return new Result(200);
        else
            return new Result(400);
    }
    public List<SectionMap> getSection(int index){
        return  sectionSqlDao.getList(index);
    }
    public List<SectionMap> getKeySection(String keyname, int index){
        return  sectionSqlDao.findKeyList(keyname,index);
    }
    public int getMaxSectionId(){
        return sectionSqlDao.findMaxId();
    }
    public int getSumSectionId(){return sectionSqlDao.findSum();}
    public int getSumKeySection(String key){return sectionSqlDao.findKeySum(key);}
    public Result insertIntoSection(Section M){
        if(Objects.equals(M.getStatus(), "正常"))
            M.setGrade("Green");
        else if(Objects.equals(M.getStatus(), "警告"))
            M.setGrade("Yellow");
        else if(Objects.equals(M.getStatus(),"异常"))
            M.setGrade("Red");
        int count;
        if(sectionDao.findAllById(M.getId()).isEmpty()) {
            sectionSqlDao.crateMonitorData(M.getId());
            count = sectionSqlDao.insertIntoSection(M);
        }
        else
             count= sectionSqlDao.updateSection(M);
        if(count>0)
           return new Result(200);
        else
           return new Result(400);
    }
    public void deleteSection(int id){
        sectionSqlDao.deleteSection(id);
    }

}
